<template>
  <div>
    <v-dialog title="新建应付调整" :show.sync="dialogVisible" :destroy-on-close="true" width="1200px" @beforeClose="beforeClose">
      <div class="card-box">
        <el-table :data="tableData" align="center" v-if="isRouterAlive">
          <el-table-column prop="propertyNames" label="物业名称" width="120"></el-table-column>
          <el-table-column prop="customerName" label="出租方" width="120"></el-table-column>
          <el-table-column prop="chargeProjectName" label="付款项目" width="120"></el-table-column>
          <el-table-column prop="costPeriodStart" label="费用周期" width="180">
            <template slot-scope="scope">
              <span>{{scope.row.costPeriodStart}}至{{scope.row.costPeriodEnd}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="payableDate" label="应付日期" width="120"></el-table-column>
          <el-table-column prop="payableMoney" label="合同应付金额（元）" width="120"></el-table-column>
          <el-table-column prop="adjustmentAmount" label="本次调整金额（元）" width="120">
            <template slot-scope="scope">
              <el-input size="small" v-model="scope.row.adjustmentAmount" placeholder="请输入内容" @change="handleChangeAmount"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="adjustedAmount" label="调整后金额（元）" width="120"></el-table-column>
          <!--<el-table-column prop="remarks" label="备注" width="120">
                        <template slot-scope="scope">
                            <el-input size="small" v-model="scope.row.remarks" placeholder="请输入内容"></el-input>
                        </template>
          </el-table-column>-->
        </el-table>
      </div>
      <div class="card-box">
        <div class="card-title">备注</div>
        <div class="handling-opinions">
          <el-input v-model="remarks" placeholder="请输入" maxlength="200" class="full"></el-input>
        </div>
      </div>
      <div class="card-box">
        <div class="card-title">
          <div>
            <span class="required">*</span>上传文件
          </div>
        </div>
        <div class="handling-opinions">
          <v-upload ref="adjustment" :busId="projectId" modelType="finance" @onSuccess="onSuccess" isShowFileList :isView="false" multiple v-model="fileList" />
        </div>
      </div>
      <div class="card-box">
        <div class="card-title">
          <div>
            <span class="required">*</span>办理意见
          </div>
        </div>
        <div class="handling-opinions">
          <el-input type="textarea" v-model="opinion" maxlength="100" placeholder="请输入" class="full"></el-input>
        </div>
      </div>
      <span slot="foot">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button v-throttle="[commit]">保 存</el-button>
        <el-button v-throttle="[commitReview]">保存并提交复核</el-button>
      </span>
    </v-dialog>
  </div>
</template>

<script>
import { addAdjustment, getObj } from "../../api/lessor-payable"
export default {
  name: "edit-adjustment",
  data () {
    return {
      dialogVisible: false,
      tableData: [],
      fileList: [],
      projectId: '',
      opinion: '',
      remarks: '',
      isRouterAlive: true,
      form: {
        adjustmentAmount: 0,
        adjustmentDate: '',
        adjustmentId: '',
        adjustmentReason: '',
        adjustmentType: '',
        createBy: 0,
        createName: '',
        createTime: '',
        isDeleted: 0,
        operateBy: 0,
        operateName: '',
        operateTime: '',
        opinion: '',
        payableId: '',
        remarks: '',
        sort: 0,
        status: '',
        customerId: ''
      }
    }
  },
  methods: {
    show (item) {
      this.dialogVisible = true
      this.projectId = item.adjustmentId
      getObj(item.payableId).then(res => {
        if (res.data.code === 0) {
          this.tableData.push(res.data.data)
          this.tableData[0].adjustmentAmount = ''
        }
      })
      this.isRouterAlive = false
      this.$nextTick(() =>
        this.isRouterAlive = true
      )
    },
    handleChangeAmount (val) {
      let reg = /^(([1-9]\d*)|(0))(\.\d{1,2})?$/;
      if (!reg.test(val)) {
        this.$message.error('数值格式错误（最多保留两位小数）')
        this.tableData[0].adjustmentAmount = ''
        this.tableData[0].adjustedAmount = ''
      } else {
        this.tableData[0].adjustedAmount = Number(this.tableData[0].payableMoney) + Number(this.tableData[0].adjustmentAmount)
        this.isRouterAlive = false
        this.$nextTick(() =>
          this.isRouterAlive = true
        )
      }

      /* if (this.tableData[0].adjustmentAmount > this.tableData[0].payableMoney) {
           this.$message.error('调整金额不能大于应付金额！')
           this.tableData[0].adjustmentAmount = ''
           this.tableData[0].adjustedAmount = ''
       }*/
    },
    handleCancel () {
      this.dialogVisible = false
      this.tableData = []
      this.opinion = ''
      this.$emit('handleClose')
    },
    commit () {
      if (this.tableData[0].adjustmentAmount === '') {
        this.err('请填写本次调整金额！')
      } else if (this.tableData[0].adjustmentAmount === 0) {
        this.err('调整金额不能为0！')
      } else if (this.opinion === '' || this.opinion === null || this.opinion === undefined) {
        this.$message.error('办理意见不能为空！')
      } else if (this.fileList.length <= 0) {
        this.$message.error('应付调整必须上传文件！')
      } else {
        this.form = this.tableData[0]
        this.form.adjustmentStatus = '10'
        this.$refs.adjustment.submit()
      }
    },
    commitReview () {
      if (this.tableData[0].adjustmentAmount === '') {
        this.err('请填写本次调整金额！')
      } else if (this.tableData[0].adjustmentAmount === 0) {
        this.err('调整金额不能为0！')
      } else if (this.opinion === '' || this.opinion === null || this.opinion === undefined) {
        this.$message.error('办理意见不能为空！')
      } else if (this.fileList.length <= 0) {
        this.$message.error('应付调整必须上传文件！')
      } else {
        this.form = this.tableData[0]
        this.form.adjustmentStatus = '11'
        this.$refs.adjustment.submit()
      }
    },
    //文件上传
    async onSuccess (response, file, fileList) {
      this.form.opinion = this.opinion
      this.form.remarks = this.remarks
      this.form.adjustmentId = response.data
      const res = await addAdjustment(this.form)
      if (res.data.code == 0) {
        this.$message.success(`提交成功!`);
        this.dialogVisible = false;
        this.$emit('resetList')
      } else {
        this.$message.error(res.data.msg)
      }
    },
    beforeClose (done) {
      this.$emit('beforeClose')
    },
  },
}
</script>

<style scoped lang="scss">
.card-box {
  margin-top: 20px;
  .handling-opinions {
    padding-top: 20px;
    .el-textarea__inner {
      border: 0.5px solid #ccc;
    }
  }
  .card-title {
    .required {
      color: #f67979;
      margin-right: 8px;
    }
  }
}
</style>
